package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.CouponDetailsDao;
import com.baichang.taoshunda.dao.PromotionDao;
import com.baichang.taoshunda.entity.Promotion;
import com.baichang.taoshunda.service.CouponService;
import com.baichang.taoshunda.service.PromotionService;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by ${wushenglu} on 17/09/08.
 */

@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    PromotionDao promotionDao;

    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponDetailsDao couponDetailsDao;

    @Override
    public List<Map<String, Object>> getPromotionByBussId(int busId) {
        return promotionDao.getPromotionByBussId(busId);
    }

    /**
     * 修改商家优惠活动
     *
     * @param id
     * @param name
     * @return
     */
    @Override
    public int updateBusinessActivity(Integer id, String name) {
        return promotionDao.updateBusinessActivity(id, name);
    }

    /**
     * 删除商家优惠活动
     *
     * @param id
     * @return
     */
    @Override
    public int deleteActivity(Integer id) {
        return promotionDao.deleteById(id);
    }

    /**
     * 添加商家优惠
     *
     * @param businessId
     * @param name
     * @return
     */
    @Override
    public long addBusinessActivity(Integer businessId, String name) {
        Promotion promotion = new Promotion();
        promotion.setCreated(new Date());
        promotion.setBussId(businessId);
        promotion.setName(name);
        promotion.setMinMoney(BigDecimal.valueOf(0));
        promotion.setSubMoney(BigDecimal.valueOf(0));
        return promotionDao.save(promotion);
    }

    /**
     * 根据状态查询商家的活动
     *
     * @param bussId 商家编号
     * @param state  状态
     *               0：待生效
     *               1：已生效
     *               2：已结束
     * @return
     */
    @Override
    public List<Map<String, Object>> findPromotionByState(Integer bussId, Integer state) {
        List<Map<String, Object>> returnMapList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> maps = promotionDao.findPromotionByState(bussId, state);
        if (maps != null && maps.size() > 0) {
            List<String> kids = new ArrayList<String>();
            for (Map<String, Object> map : maps) {
                Integer type = ConvertTypeUtils.obj2Integer(map.get("type"));
                if (type == 1) {
                    String kid = map.get("kid").toString();
                    Map<String, Object> couponMap = new HashMap<String, Object>();
                    if (!kids.contains(kid)) {
                        kids.add(kid);
                        couponMap.put("id", map.get("id"));
                        couponMap.put("name", map.get("name"));
                        couponMap.put("bussId", map.get("bussId"));
                        couponMap.put("minMoney", map.get("minMoney"));
                        couponMap.put("subMoney", map.get("subMoney"));
                        couponMap.put("created", map.get("created"));
                        couponMap.put("startTime", map.get("startTime"));
                        couponMap.put("endTime", map.get("endTime"));
                        couponMap.put("validityDays", map.get("validityDays"));
                        couponMap.put("type", map.get("type"));
                        couponMap.put("state", map.get("state"));
                        List<Map<String, Object>> moneyMapList = couponDetailsDao.findCouponDetailsByKid(kid);
                        couponMap.put("moneyMapList", moneyMapList);
                        returnMapList.add(couponMap);
                    }
                } else {
                    returnMapList.add(map);
                }
            }
        }
        return returnMapList;
    }


    /**
     * 创建优惠活动
     *
     * @param bussId    商家编号
     * @param name      活动名称
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @Override
    public boolean addPromotion(Integer bussId, String name, String beginTime, String endTime) throws ServiceException {
        int count = promotionDao.getPromotionCountByBussId(bussId);
        if (count < 3) {
            Promotion promotion = new Promotion();
            promotion.setBussId(bussId);
            promotion.setCreated(new Date());
            promotion.setName(name);
            promotion.setStartTime(ConvertTypeUtils.obj2DateFormat(beginTime));
            promotion.setEndTime(ConvertTypeUtils.obj2DateFormat(endTime));
            return promotionDao.save(promotion) > 0;
        } else {
            throw new ServiceException("已达有效期内活动申请上限！");
        }

    }
}
